package com.dayuanit.dy15.ebook.bookshop.service.impl;

import com.dayuanit.dy15.ebook.bookshop.dto.CommodityDTO;
import com.dayuanit.dy15.ebook.bookshop.dto.CommodityHomeDTO;
import com.dayuanit.dy15.ebook.bookshop.entity.Commodity;
import com.dayuanit.dy15.ebook.bookshop.entity.Navigation;
import com.dayuanit.dy15.ebook.bookshop.mapper.CommodityMapper;
import com.dayuanit.dy15.ebook.bookshop.mapper.NavigationMapper;
import com.dayuanit.dy15.ebook.bookshop.redis.RedisService;
import com.dayuanit.dy15.ebook.bookshop.service.CommodityService;
import com.dayuanit.dy15.ebook.bookshop.util.BookUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Autowired
    private NavigationMapper navigationMapper;

    @Resource(name = "redisServiceTempleImpl")
    private RedisService redisService;

    @Value("${ebook.picture.url}")
    private String pictureUrl;


    @Override
    public List<CommodityDTO> loadCommondityByNavId(Long firstNavId, Long secondNavId, Long thirdNavId) {
        List<Commodity> commodities = commodityMapper.listByNavId(firstNavId, secondNavId, thirdNavId);
        List<CommodityDTO> dtos = new ArrayList<>(commodities.size());
        for (Commodity commodity : commodities) {
            CommodityDTO commodityDTO = BookUtils.converter2DTO(pictureUrl, commodity);
            dtos.add(commodityDTO);
        }
        return dtos;
    }

    @Override
    public CommodityDTO loadBookDetail(long bookId) {
        Commodity commodity = commodityMapper.selectByPrimaryKey(bookId);
        CommodityDTO commodityDTO = BookUtils.converter2DTO(pictureUrl, commodity);

        //数组转List集合    集合转数组？  面试题
        List<Long> navIds = Arrays.asList(commodity.getFirstNavId(), commodity.getSecondNavId(), commodity.getThirdNavId());

        //根据导航栏ID查询名称
        List<Navigation> navigations = navigationMapper.listById(navIds);
        navigations.forEach(nav -> {
            if (nav.getLevel() == 1) {
                commodityDTO.setFistNavName(nav.getNavName());
            }
            if (nav.getLevel() == 2) {
                commodityDTO.setSecondNavName(nav.getNavName());
            }
            if (nav.getLevel() == 3) {
                commodityDTO.setThirdNavName(nav.getNavName());
            }
        });

        return commodityDTO;
    }


    @Override
    public List<CommodityHomeDTO> loadHomeBook() {

        //先到缓存查询首页商品
        List<CommodityHomeDTO> books = redisService.zrevRange(0, 20);

        List<Commodity> commodities = Collections.emptyList();

        if (!books.isEmpty()) {
            List<Long> bookIds = books.stream().map(CommodityHomeDTO::getCommoId).collect(Collectors.toList());
            commodities = commodityMapper.listByIds(bookIds);
        } else {
            commodities = commodityMapper.listTop20();
        }

        List<CommodityHomeDTO> dtos = new ArrayList<>(commodities.size());

        Map<Long, CommodityHomeDTO> bookMap = books.stream().collect(Collectors.toMap(book -> book.getCommoId(), book -> book));


        for (Commodity commodity : commodities) {
            CommodityHomeDTO commodityHomeDTO = bookMap.get(commodity.getId());
            commodityHomeDTO.setAuthor(commodity.getAuthor());
            commodityHomeDTO.setCommoName(commodity.getCommoName());
            commodityHomeDTO.setPictureUrl(pictureUrl + commodity.getPictureName());
            commodityHomeDTO.setPublisher(commodity.getPublisher());
            commodityHomeDTO.setStock(commodity.getStock());
            commodityHomeDTO.setWordNum(commodity.getWordNum());
            dtos.add(commodityHomeDTO);
        }

        Collections.sort(dtos, new Comparator<CommodityHomeDTO>() {
            @Override
            public int compare(CommodityHomeDTO o1, CommodityHomeDTO o2) {
                return (int)(o2.getScore() - o1.getScore());
            }
        });



        return dtos;
    }
}
